import React, { useState } from 'react';
import { Form, Input, Button, message } from 'antd';
import userService from '@/services/userService';
import { FormCard } from '@/components/common';

interface ResetPasswordFormProps {
  userId: number;
  onSuccess: () => void;
  onCancel: () => void;
}

const ResetPasswordForm: React.FC<ResetPasswordFormProps> = ({ userId, onSuccess, onCancel }) => {
  const [form] = Form.useForm();
  const [loading, setLoading] = useState(false);

  const handleSubmit = async (values: { password: string; password_confirm: string }) => {
    setLoading(true);
    try {
      await userService.resetUserPassword(userId, values);
      message.success('密码重置成功');
      form.resetFields();
      onSuccess();
    } catch (error) {
      console.error('Failed to reset password:', error);
    } finally {
      setLoading(false);
    }
  };

  return (
    <FormCard
      title="重置密码"
      form={form}
      onFinish={handleSubmit}
      actions={[
        <Button key="cancel" onClick={onCancel}>取消</Button>,
        <Button key="submit" type="primary" htmlType="submit" loading={loading}>
          重置密码
        </Button>
      ]}
    >
      <Form.Item
        name="password"
        label="新密码"
        rules={[
          { required: true, message: '请输入新密码' },
          { min: 6, message: '密码长度不能少于6个字符' }
        ]}
      >
        <Input.Password placeholder="请输入新密码" />
      </Form.Item>

      <Form.Item
        name="password_confirm"
        label="确认新密码"
        dependencies={['password']}
        rules={[
          { required: true, message: '请确认新密码' },
          ({ getFieldValue }) => ({
            validator(_, value) {
              if (!value || getFieldValue('password') === value) {
                return Promise.resolve();
              }
              return Promise.reject(new Error('两次输入的密码不一致'));
            },
          }),
        ]}
      >
        <Input.Password placeholder="请再次输入新密码" />
      </Form.Item>
    </FormCard>
  );
};

export default ResetPasswordForm; 